/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.huawei.spare.part.replace.views.prepare.project;

import com.huawei.spare.part.replace.Constants;
import com.huawei.spare.part.replace.framework.SmartContentPresenter;
import com.huawei.spare.part.replace.framework.SmartDataBus;
import com.huawei.spare.part.replace.models.Project;
import com.huawei.spare.part.replace.models.Solution;
import com.huawei.spare.part.replace.models.Switch;
import com.huawei.spare.part.replace.utils.Application;
import com.huawei.spare.part.replace.utils.JsonMapper;
import com.huawei.spare.part.replace.views.widgets.toast.Toast;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @author Woo
 */
public abstract class ProjectBasePresenter extends SmartContentPresenter {

    @FXML Label spareBoardType;
    @FXML Label spareSoftwareVersion;
    @FXML Label spareCpldVersion;

    @FXML Label defectiveBoardType;
    @FXML Label defectiveSoftwareVersion;
    @FXML Label defectiveCpldVersion;
    @FXML Label defectiveIsStacked;

    @FXML GridPane installPackages;
    @FXML VBox riskContainer;


    protected void renderProjectContent(Project project) {
        final Project.Defective defective = project.getDefective();
        final Project.Spare spare = project.getSpare();
        final Solution solution = project.getSolution();

        // 坏件
        defectiveBoardType.setText(defective.getBoardType());
        defectiveSoftwareVersion.setText(defective.getSoftwareVersion());
        defectiveCpldVersion.setText(defective.getCpldVersion());
        defectiveIsStacked.setText(getString(defective.getIsStacked() ? "constants.dict.yes" : "constants.dict.no"));

        // 备件
        spareBoardType.setText(spare.getBoardType());
        spareSoftwareVersion.setText(spare.getSoftwareVersion());
        spareCpldVersion.setText(spare.getCpldVersion());

        // 安装包
        ObservableList<Node> children = this.installPackages.getChildren();
        final Project.DownloadPackages downloadPackages = project.getDownloadPackages();
        if (downloadPackages.getFabric() != null) {
            ((Label) children.get(3 * 1 + 1)).setText(downloadPackages.getFabric().getVersion());
            ((Label) children.get(3 * 1 + 2)).setText(downloadPackages.getFabric().getPackageName());
        }
        if (downloadPackages.getBase() != null) {
            ((Label) children.get(3 * 2 + 1)).setText(downloadPackages.getBase().getVersion());
            ((Label) children.get(3 * 2 + 2)).setText(downloadPackages.getBase().getPackageName());
        }
        if (downloadPackages.getCpld() != null) {
            ((Label) children.get(3 * 3 + 1)).setText(downloadPackages.getCpld().getVersion());
            ((Label) children.get(3 * 3 + 2)).setText(downloadPackages.getCpld().getPackageName());
        }
        if (downloadPackages.getBmc() != null) {
            ((Label) children.get(3 * 4 + 1)).setText(downloadPackages.getBmc().getVersion());
            ((Label) children.get(3 * 4 + 2)).setText(downloadPackages.getBmc().getPackageName());
        }

        // 风险信息&操作建议
        addRiskLabel("common.p.title.risk", "h3");
        solution.getRisks().stream().forEachOrdered(risk -> {
            addRiskLabel(risk, "span");
        });

        addRiskLabel("common.p.title.suggestion", "h3");
        solution.getSuggestions().stream().forEachOrdered(suggestion -> {
            addRiskLabel(suggestion, "span");
        });
    }

    private void addRiskLabel(String s, String styleClass) {
        final Label label = new Label(getString(s));
        label.setWrapText(true);
        label.getStyleClass().add(styleClass);
        riskContainer.getChildren().add(label);
    }

}
